/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.ivanceras.client.widgets.AdMenuUi;
import com.ivanceras.client.widgets.AdWindowUi;
import com.ivanceras.client.widgets.DataImporterUI;
import com.ivanceras.client.widgets.MainPanelUi;
import com.ivanceras.client.widgets.WindowController;
import com.ivanceras.client.widgets.XmlReaderUI;
import com.ivanceras.shared.bo.Ad_window;

public class Ivanceras implements EntryPoint {
	private final DataServiceAsync dataService = GWT.create(DataService.class);

	HTMLPanel mainPanel = new HTMLPanel("");
	Button importLink = new Button("Import Module");
	Button windowsButton = new Button("Windows");
	Button xmlReader = new Button("XML Reader");
	MainPanelUi mainPanelUi = new MainPanelUi();

	public void onModuleLoad() {
		RootPanel.get().add(importLink);
		RootPanel.get().add(windowsButton);
		RootPanel.get().add(xmlReader);
		RootPanel.get().add(mainPanel);
		importLink.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				DataImporterUI importer = new DataImporterUI();
				mainPanel.clear();
				mainPanel.add(importer);
			}
		});
		windowsButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				retrieveWindows();
				mainPanel.clear();
				mainPanel.add(mainPanelUi);
			}
		});
		xmlReader.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				mainPanel.clear();
				XmlReaderUI reader = new XmlReaderUI();
				mainPanel.add(reader);
			}

		});
	}

	void retrieveWindows() {
		System.out.println("Retrieving window list: ");
		dataService.retrieveWindowList(new AsyncCallback<Ad_window[]>() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Ad_window[] result) {
				for (final Ad_window win : result) {
					AdMenuUi menuUi = new AdMenuUi(win.getName());
					menuUi.setClickHanlder(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							showWindow(win);
						}
					});
					MainPanelUi.get().addMenu(menuUi);
				}
			}

		});
	}

	private void showWindow(final Ad_window win) {
		WindowController wc = new WindowController();
		wc.load(win, new AsyncCallback<AdWindowUi>() {

					@Override
					public void onSuccess(AdWindowUi result) {
						MainPanelUi.get().addWindow(win.getName(), result);
					}

					@Override
					public void onFailure(Throwable caught) {
						System.err.println("Error retrieving window "
								+ win.getName());
						caught.printStackTrace();
					}
				});
	}

}
